package com.ruoyi.order.utils;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.ruoyi.order.domain.TbOrder;
import org.springframework.stereotype.Component;

/**
 * 支付宝工具包
 *
 * @author 12692
 */
@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDFjaaOU/KyS8qn3SGeFwRdKn0YjCxxHvzuQrWriwMBEE13VRWdqUfDWbBaWGJb1S6zZW9b6DHT1TfzPqX+BhnnEaYATKMQUINtgBihepZFg9jLklpn/7zcGyKUQA9sNuaF2/kguvoNKupmnTrZxh0YdzXWDWK5AiQ14DPDPQ7hdqpdhxwDan3WU8LlIwpjQ8DmukrQiI8xnVIyPL4q5OdaSfR0X/wH9y9fgCv2UaTcSSuiMgoq0vHZ53dUFJhyFnMJXnlcQT6edeeTw8Hv2yfJes+l0NYa/ecJuQPEkhlMbN2Dw9AkbHWdm873aBuMo+CE33bbMTHNfkK0Mh8T0ApBAgMBAAECggEABq4Le7SK+ExnxdVMVbQhiaTuJe5XclvB4ayPyAUjTN3oQKi32mTm8EwQEn36lXBX6+j6Ivlu9eK3yZnMsxIxvgZyvn/Y46xVRVaXfp5Ohns20WtZczrKgAbK3Iht/RK+KLiPa/PmkJtq4Z1faA2y3ijpwRsiw263GrtCBNADuMvjLeclndxik59LqOrCoon7rZNqoBnIPAY7DbjW9E7Z2JogzRFk/E855zutfWo6coj86H5k84LlI9mqyELzeFjX9K6BJwH5JLat0kFmovAppDvcasFfHoMmIcclFUYF7eA9gabNOsYOqZMDPW7UmVkKrzjw+qs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S30u2jWYHs78gdU4Y9oIr/8wcVFT/gLuqjSiSZWn21ceN69M0Kdc4YkaWouC0irKTmJEs27JSJX+d+fopgLRV6/e2ssGkWthhJnOY71UpiGz2nxoqVBVcskylTAidDe+UfYSdFaEsHe7JRhZ4IfqBzoUY/eqJhxvhwWMTtMgn0djat/pvg3KjyTbxNCyNzDpCGUg/hUP+QbX7bqj4ZF7N4tI1Hy9QIDAQAB";

    //生成支付订单接口
    public String pay(TbOrder payData) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000122628405");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setReturnUrl("http://localhost:8888/#/zfbPay");
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(payData.getOrderId());
        model.setTotalAmount(payData.getOrderMoney().toString());
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        return response.getBody();
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000122628405");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }
}
